/*
 * RTMLHelperBean.java is part of htn-core.
 *
 * RTMLHelperBean.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * RTMLHelperBean.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with RTMLHelperBean.java.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.htn.rtml;

import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.htn.rtml.RTMLHelper;
import org.htn.rtml.RTMLMode;
import org.htn.util.DateUtil;
import org.htn.util.XPathExtractor;
import org.htn.util.XmlTransformer;
import org.htn.util.XmlTransformerException;
import org.jdom.Attribute;
import org.jdom.Element;
import org.joda.time.DateTime;

public class RTMLHelperBean implements RTMLHelper<String> {

	private static final Logger log = Logger.getLogger(RTMLHelperBean.class);

	private static final String EXPIRES = "//RTML/@expires";
	private static final String MODE = "//RTML/@mode";
	private static final String OBSERVATION_IDS =
		"//RTML/Schedule/Observation/@id"; 
	private static final String RTMLUID = "//RTML/@uid";
	private static final String RESPOND_TO = "//RTML/RespondTo";
	private static final String CONTACT_USERNAME = "//RTML/Contact/Username";
	private static final String PROJECT_ID = "//RTML/Project/@ProjectID";
	private static final String TARGET_ID = "//RTML/Target/@id";
	private static final String IMAGEDATA_URI = "//RTML/ImageData/Uri";
	
	private DateUtil dateUtil = new DateUtil();
	private XmlTransformer xt = new XmlTransformer();
	private XPathExtractor xpx = new XPathExtractor();
	
	public DateTime getExpires(String rtml) {
		return this.dateUtil.parseString(getAttributeAsString(rtml,EXPIRES));
	}

	public RTMLMode getMode(String rtml) {
		return RTMLMode.resolve(getAttributeAsString(rtml,MODE));
	}

	public Set<String> getObservationIdSet(String rtml) {
		return getAttributesAsString(rtml,OBSERVATION_IDS);
	}

	public Set<String> getContactUsername(String rtml) {
		return getElementContentSetAsString(rtml,CONTACT_USERNAME);
	}

	public String getProjectID(String rtml) {
		return getAttributeAsString(rtml,PROJECT_ID);
	}

	public String getTargetId(String rtml) {
		return getAttributeAsString(rtml,TARGET_ID);
	}
	
	public String getRTMLUid(String rtml) {
		return getAttributeAsString(rtml,RTMLUID);
	}

	public Set<String> getRespondToSet(String rtml) {
		return getElementContentSetAsString(rtml,RESPOND_TO);
	}
	
	public Set<String> getImagedataUriSet(String rtml) {
		return getElementContentSetAsString(rtml,IMAGEDATA_URI);
	}

	private Set<String> getElementContentSetAsString(String rtml,
			String xpath) {
		Set<String> elementContents = new LinkedHashSet<String>();
		try {
			Set<Element> elems = xpx.getElements(
					xt.toDocument(rtml).getRootElement(),
					xpath);
			for (Element el : elems) {
				elementContents.add(el.getText());
			}
		} catch (XmlTransformerException e) {
			log.error(e,e);
		}
		return elementContents;
	}
	
	private String getAttributeAsString(String rtml, String xpath) {
		String attValue = null;
		try {
			attValue = xpx.getSingleAttribute(
					xt.toDocument(rtml).getRootElement(),
					xpath)
					.getValue();
		} catch (XmlTransformerException e) {
			log.error(e,e);
		}
		return attValue;
	}
	
	private Set<String> getAttributesAsString(String rtml, String xpath) {
		Set<String> atts = new LinkedHashSet<String>();
		try {
			Set<Attribute> attSet = xpx.getMultipleAttributesForElementName(
					xt.toDocument(rtml).getRootElement(),
					xpath);
			for (Attribute a : attSet) {
				atts.add(a.getValue());
			}
		} catch (XmlTransformerException e) {
			e.printStackTrace();
		}
		return atts;
	}
	
}
